package org.stone.project.tx.customer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.stone.project.tx.config.RabbitConfig;
import org.stone.project.tx.service.MsgSendUserTypeHandlerAdapter;

import java.io.IOException;

@Component
@Slf4j
public class MsgCustomer {
    @Autowired
    private RabbitConfig rabbitConfig;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private MsgSendUserTypeHandlerAdapter msgSendUserTypeHandlerAdapter;

    @RabbitListener(queues = "#{rabbitConfig.queueNameUser}")
    public void userConsumorMsg(Message msg, Channel channel) throws IOException {
        org.stone.project.tx.model.Message message = objectMapper.readValue(new String(msg.getBody()), org.stone.project.tx.model.Message.class);
        msgSendUserTypeHandlerAdapter.sendMsg(message);
    }

    @RabbitListener(queues = "#{rabbitConfig.queueNameRole}")
    public void roleConsumorMsg(Message msg, Channel channel) throws IOException {
        org.stone.project.tx.model.Message message = objectMapper.readValue(new String(msg.getBody()), org.stone.project.tx.model.Message.class);
        msgSendUserTypeHandlerAdapter.sendMsg(message);
    }

    @RabbitListener(queues = "#{rabbitConfig.queueNameGroup}")
    public void groupConsumorMsg(Message msg, Channel channel) throws IOException {
        org.stone.project.tx.model.Message message = objectMapper.readValue(new String(msg.getBody()), org.stone.project.tx.model.Message.class);
        msgSendUserTypeHandlerAdapter.sendMsg(message);
    }

    @RabbitListener(queues = "#{rabbitConfig.queueNameAll}")
    public void allConsumorMsg(Message msg, Channel channel) throws IOException {
        org.stone.project.tx.model.Message message = objectMapper.readValue(new String(msg.getBody()), org.stone.project.tx.model.Message.class);
        msgSendUserTypeHandlerAdapter.sendMsg(message);
    }
}
